﻿@using LaserMark.DataAccess.Service
@using System.Drawing
@using System.Drawing.Imaging
@using System.Text.RegularExpressions

<Alert ShowBar="true" ShowBorder="true" Color="BootstrapBlazor.Components.Color.Primary">
    <div>打码测试前，请务必使镜头处于最佳焦距处</div>
</Alert>

<div class="code-template">
    <Table TItem="MarkTemplate" IsBordered="true" IsStriped="true" OnQueryAsync="OnQueryAsync"
           ShowExtendButtons="true" ShowExtendDeleteButton="true" ShowExtendEditButton="false" IsFixedHeader="true" Height="450" OnDeleteAsync="OnDeleteAsync" DeleteButtonToastContent="是否删除此模板？">
        <RowButtonTemplate>
            <TableCellButton IsAsync="true" Icon="fa fa-print" Text="打码" OnClick="e => OnPreview(context)"></TableCellButton>
        </RowButtonTemplate>
        <TableColumns Context="context">
            <TableColumn @bind-Field="@context.Order" Text="标序"></TableColumn>
            <TableColumn @bind-Field="@context.TemplateName" Text="模板名称"></TableColumn>
            <TableColumn @bind-Field="@context.Type" Text="模板种类"></TableColumn>
            <TableColumn @bind-Field="@context.MatchingNo" Text="模板匹配号">
                <Template Context="v">
                    @{
                        if (v.Row.MatchingNo != null)
                        {
                            var tempArr = v.Row.MatchingNo!.Split(",");
                            var str = "";
                            foreach (var match in tempArr)
                            {
                                if (match == "0")
                                {
                                    str += "客户名称" + ",";
                                }

                                if (match == "1")
                                {
                                    str += "订单编号" + ",";
                                }

                                if (match == "2")
                                {
                                    str += "流程卡号" + ",";
                                }

                                if (match == "3")
                                {
                                    str += "小片宽" + ",";
                                }

                                if (match == "4")
                                {
                                    str += "小片高" + ",";
                                }

                                if (match == "5")
                                {
                                    str += "楼层号" + ",";
                                }

                                if (match == "6")
                                {
                                    str += "货架号";
                                }
                            }

                            <Tooltip Title="@str.Remove(str.Length - 1)" Placement="Placement.Top">
                                <div class="text-center">@str.Remove(str.Length - 1)</div>
                            </Tooltip>
                        }
                        else
                        {
                            <div></div>
                        }
                    }
                </Template>
            </TableColumn>
            <TableColumn @bind-Field="@context.XMargin" Text="模板宽(mm)"></TableColumn>
            <TableColumn @bind-Field="@context.YMargin" Text="模板高(mm)"></TableColumn>
            <TableColumn @bind-Field="@context.ReMark" Text="备注"></TableColumn>
        </TableColumns>
    </Table>
</div>

<DialogFooter></DialogFooter>
@code
{
    [Inject]
    [NotNull]
    private ICodeTemplateService? CodeTemplateService { get; set; }

    [Inject]
    [NotNull]
    private DialogService? DialogService { get; set; }

    [Inject]
    [NotNull]
    private IPrint? Print { get; set; }

    private async Task<bool> OnDeleteAsync(IEnumerable<MarkTemplate> marks)
    {
        foreach (var item in marks)
        {
            if (File.Exists(item.TemplateName))
            {
                File.Delete(item.TemplateName);
            }

            await CodeTemplateService.Delete(item);
        }
        return true;
    }

    private async Task<QueryData<MarkTemplate>> OnQueryAsync(QueryPageOptions options)
    {
        var items = await CodeTemplateService.GetAll();

        return new QueryData<MarkTemplate>()
            {
                Items = items
            };
    }


    private async Task OnPreview(MarkTemplate mark)
    {
        await Print.CreateEntMgrAsync();
        await Print.LoadTemplateAsync(mark.TemplatePath!, out _, out _);
        var aa = await Print.Copy();
        var res = await Print.MarkerMarkEnt2Async(aa);
        if (res == 0)
        {
            await Print.Dispose();
        }
    }
}
